<?php
class ayNoMemes {
    public function addMeme($imagen, $descripcion, $fechaPub){
        if (isset ($_FILES["imagen"])){
            $imagesDirectory = "/projects/masmedio2013/webroot/fotos/";
            //$imagesDirectory = "/home/content/a/d/m/adminmasmedio/html/fotos/";

            $temporaryName = $_FILES["imagen"]["tmp_name"];
            $fileName = $_FILES["imagen"]["name"]; //echo $fileName . "<br />\n";
            $fileType = $_FILES["imagen"]["type"]; //echo $fileType . "<br />\n";
            $fileSize = $_FILES["imagen"]["size"]; //echo $fileSize . "<br />\n";

            if (strpos ($fileType, "gif") || strpos ($fileType, "jpeg") || strpos ($fileType, "jpg") || strpos ($fileType, "bmp") || strpos ($fileType, "png")){
                if (file_exists ($imagesDirectory . $fileName)){
                    $fileSuffix = 0;
                    for (;;){
                        $dotPosition = strrpos ($_FILES["imagen"]["name"], ".");
                        $newFileName = substr ($_FILES["imagen"]["name"], 0, $dotPosition);
                        $newFileName .= $fileSuffix;
                        $newFileName .= substr ($_FILES["imagen"]["name"], $dotPosition);;
                        if (!(file_exists ($imagesDirectory . $newFileName)))
                            break;
                        $fileSuffix += 1;
                    }
                    $chanwiz = $imagesDirectory . $newFileName;
                    move_uploaded_file ($temporaryName, $chanwiz);
                    $imagen = $newFileName;
                }
                else{
                    $chanwiz = $imagesDirectory.$fileName;
                    move_uploaded_file ($temporaryName, $chanwiz);
                    $imagen = $fileName;
                }
            }
        }

        $SQL = "INSERT INTO ay_no_memes (foto, descripcion, fechaPublicacion) "
            ."VALUES ('".$imagen."', '".$descripcion."', '".$fechaPub."')";
        //	echo $SQL;
        $Q = mysql_query($SQL) or die(mysql_error());
    }

    public function getLast20AyNoMemes(){
        $SQL = "SELECT * FROM ay_no_memes WHERE status = 'A' AND fechaPublicacion<=curdate()
				ORDER BY fechaPublicacion DESC, id DESC LIMIT 20";

        $Q = mysql_query($SQL)or die(mysql_error());

        if(mysql_num_rows($Q)>0){
            $arrRes = Array();
            $i = 0;
            while($row = mysql_fetch_array($Q)){
                $arrRes[$i]['id'] = $row["id"];
                $arrRes[$i]['descripcion'] = substr(html_entity_decode($row["descripcion"], ENT_COMPAT, "UTF-8"), 0, 45)."...";
                $arrRes[$i]['fechaPublicacion'] = $row["fechaPublicacion"];
                $i++;
            }
            return $arrRes;
        }else{
            $ERR = "No hay memes en este momento.";
            return $ERR;
        }
    }

    public function delAyNoMeme($id){
        $SQL = "UPDATE ay_no_memes SET status = 'S' WHERE id =".$id;
        $Q = mysql_query($SQL) or die(mysql_error());
    }

}